<?php

class GoldAction extends CommonAction
{
	private $_description = array ('1' => '金币+1', '-1' => '重复看视频~金币不加1', '-2' => '视频ID不合法');
	
	// 看视频获取金币
	public function mediaForGold(){
		$key = base64_decode(trim($_REQUEST['key']));
		$_result = $this->checkKey($key);
		if ($_result == 1)
			$_result = $this->checkRepeat($this->Core->User->u_id, $key);
		if ($_result == 1)
			$_result = $this->addGold ($key);
		
		$_rdata = array ('status' => $_result, 'msg' => $this->_description [$_result] );
		die ( json_encode ( $_rdata ) );
	}
	
	// 登录后初始化用户 然后获取金币
	public function loginToAddGold(){
		$this->Core->User = Director::Instance( '', 'Session', 'Get');
		$key = base64_decode(trim($_REQUEST['key']));
		$_result = $this->checkKey($key);
		if ($_result == 1)
			$_result = $this->checkRepeat($this->Core->User->u_id, $key);
		if ($_result == 1)
			$_result = $this->addGold ($key);
		
		$_rdata = array ('status' => $_result, 'msg' => $this->_description [$_result] );
		die ( json_encode ( $_rdata ) );
	}
	
	
	private function checkKey($key){
		if($key>1230000 && $key <1411000){
			$_result = 1;
		}else{
			$_result = -2;
		}
		return $_result;
	}

	private function checkRepeat($u_id, $key){
		$_con['u_id'] = $u_id;
		$_con['reg_key'] = $key;
		$reg = M('RecordGold')->where($_con)->find();
		return $reg ? -1 : 1 ;
	}
	
	# 看视频增加金币
	private function addGold($key){
		import('@.Util.UserGolds');
		$card_params = array('reg_gold'=> 1, 'reg_description'=>'观看视频'.$key.',金币+1', 'reg_key'=> $key);
		$card_userGolds = new UserGolds($this->Core->User, $card_params);
		$card_userGolds->save();
		return 1;
	}

	
	
}







?>